<template>
  <el-dialog title="分配权限" :visible="showDialogAssign" @close="hClose">
    <!-- 树状 -->
    <el-tree
      ref="tree"
      :data="perList"
      :props="{label:'name'}"
      show-checkbox
      default-expand-all
      check-strictly
      node-key="id"
    />
    <br>
    <el-button type="primary" size="small" @click="hSave">保存</el-button>
    <el-button size="small">取消</el-button>
  </el-dialog>
</template>

<script>
import { getRolesxiang, assignPerm } from '@/api/role'
import { getPer } from '@/api/permission'
import { arr2tree } from '@/utils'
export default {
  props: {
    showDialogAssign: {
      type: Boolean,
      required: true
    },
    curId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      perList: [] // 权限列表

    }
  },
  async created() {
    const res = await getPer()
    // console.log(res)// data
    // 数组转树
    this.perList = arr2tree(res.data, 0)
  },
  methods: {
    hClose() {
      this.$emit('update:showDialogAssign', false)
    },
    // 让父组件调用这个方法
    async assign() {
      const res = await getRolesxiang(this.curId)
      //   console.log(res.data)// data.permIds
      // element内有说明 通过 key 设置 来获取节点
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    // 点击保存
    async hSave() {
      // 获取选中节点
      const key = this.$refs.tree.getCheckedKeys()
      console.log(key)
      // 给角色分配权限 发请求
      await assignPerm({ id: this.curId, permIds: key })
      // 关闭弹框
      this.hClose()
    }
  }
}
</script>

<style>

</style>
